package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ListWithFooterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HouseOverview> mList;
    static final int TYPE_ITEM = 0;
    static final int TYPE_FOOTER = 1;
    static final int TYPE_HEADER = 2;

    //正在加载更多
    static final int LOADING_MORE = 1;
    //没有更多
    static final int NO_MORE = 2;
    //脚布局当前的状态,默认为没有更多
    int footer_state = LOADING_MORE;
    private LayoutInflater mInflater;


    public ListWithFooterAdapter(Context context, boolean loadMoreNeeded) {
        this(context, new ArrayList<HouseOverview>());
        if (!loadMoreNeeded) {
            footer_state = NO_MORE;
        }
    }

    public ListWithFooterAdapter(Context context) {
        this(context, new ArrayList<HouseOverview>());
    }

    public ListWithFooterAdapter(Context context, List<HouseOverview> list) {
        mContext = context;
        mList = list;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void clearAddData(List<HouseOverview> list) {
        mList.clear();
        addData(list);
    }

    public void addData(List<HouseOverview> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onHouseItemClick(HouseOverview houseOverview);
    }

    private OnItemClickListener mItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mItemClickListener = onItemClickListener;

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = mInflater.inflate(R.layout.list_item_house_info, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        } else if (viewType == TYPE_FOOTER) {
            //脚布局
            View view = mInflater.inflate(R.layout.list_view_footer, parent, false);
            FootViewHolder footViewHolder = new FootViewHolder(view);
            return footViewHolder;
        } else if (viewType == TYPE_HEADER) {
            View view = mInflater.inflate(R.layout.main_actions_layout, parent, false);
            HeadHolder headHolder = new HeadHolder(view);
            return headHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            final HouseOverview houseOverview = mList.get(position - 1);
            ((MyViewHolder) holder).textHouseTitle.setText(houseOverview.getName());
            ((MyViewHolder) holder).textRent.setText(houseOverview.getRent().toString()+"元/月");
            ((MyViewHolder) holder).textHoustAcreageAndFloor.setText(houseOverview.getType()
                    + " - " + houseOverview.getArea() + "平米");
            if (!TextUtils.isEmpty(houseOverview.getCover())) {
                Picasso.with(mContext).load(houseOverview.getCover())
                        .into(((MyViewHolder) holder).imageHousePhoto);
            }
            ((MyViewHolder) holder).containerHouseLable.removeAllViews();
            if (houseOverview.getEntireRent() != null && houseOverview.getEntireRent()) {
                TextView lable = (TextView) mInflater.inflate(R.layout.item_lable_text,
                        ((MyViewHolder) holder).containerHouseLable, false);
                lable.setText(R.string.rent_all);
                ((MyViewHolder) holder).containerHouseLable.addView(lable);
            }
            if (houseOverview.getOwnerRent() != null && houseOverview.getOwnerRent()) {
                TextView lable = (TextView) mInflater.inflate(R.layout.item_lable_text,
                        ((MyViewHolder) holder).containerHouseLable, false);
                lable.setText(R.string.rent_direct);
                ((MyViewHolder) holder).containerHouseLable.addView(lable);
            }
            ((MyViewHolder) holder).root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onHouseItemClick(houseOverview);
                    }
                }
            });
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (position == 0) {//如果第一个就是脚布局,,那就让他隐藏
                footViewHolder.mProgressBar.setVisibility(View.GONE);
                footViewHolder.tv_line1.setVisibility(View.GONE);
                footViewHolder.tv_line2.setVisibility(View.GONE);
                footViewHolder.tv_state.setText("");
            }
            switch (footer_state) {//根据状态来让脚布局发生改变
//        case PULL_LOAD_MORE://上拉加载
//          footViewHolder.mProgressBar.setVisibility(View.GONE);
//          footViewHolder.tv_state.setText("上拉加载更多");
//          break;
                case LOADING_MORE:
                    footViewHolder.mProgressBar.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line1.setVisibility(View.GONE);
                    footViewHolder.tv_line2.setVisibility(View.GONE);
                    footViewHolder.tv_state.setText(mContext.getString(R.string.loading));
                    break;
                case NO_MORE:
                    footViewHolder.mProgressBar.setVisibility(View.GONE);
                    footViewHolder.tv_line1.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line2.setVisibility(View.VISIBLE);
                    footViewHolder.tv_state.setText(mContext.getString(R.string.no_more_data));
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        if (position == 0) {
            return TYPE_HEADER;
        } else if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() + 2 : 0;
    }

    /**
     * 正常布局的ViewHolder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_house_photo)
        ImageView imageHousePhoto;
        @BindView(R.id.text_house_title)
        TextView textHouseTitle;
        @BindView(R.id.text_houst_acreage_and_floor)
        TextView textHoustAcreageAndFloor;
        @BindView(R.id.text_rent)
        TextView textRent;
        @BindView(R.id.text_location)
        TextView textLocation;
        @BindView(R.id.container_house_lable)
        LinearLayout containerHouseLable;
        View root;

        public MyViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        public HeadHolder(View itemView) {
            super(itemView);
            int res = R.layout.main_actions_layout;
        }
    }

    /**
     * 脚布局的ViewHolder
     */
    public static class FootViewHolder extends RecyclerView.ViewHolder {
        private ProgressBar mProgressBar;
        private TextView tv_state;
        private TextView tv_line1;
        private TextView tv_line2;


        public FootViewHolder(View itemView) {
            super(itemView);
            mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressbar);
            tv_state = (TextView) itemView.findViewById(R.id.foot_view_item_tv);
            tv_line1 = (TextView) itemView.findViewById(R.id.tv_line1);
            tv_line2 = (TextView) itemView.findViewById(R.id.tv_line2);
        }
    }

    /**
     * 改变脚布局的状态的方法,在activity根据请求数据的状态来改变这个状态
     *
     * @param state
     */
    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();
    }

}